//
//  PublicNoticeVC.m
//  New_Runway
//
//  Created by mack on 2018/2/5.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "PublicNoticeVC.h"
#import "NRWCarHomeNoticeModel.h"
#import "PublicNoticeMineCell.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "NRWCarPublicNoticeController.h"

@interface PublicNoticeVC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property(nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *noticeArray;
@end

@implementation PublicNoticeVC
-(NSMutableArray *)noticeArray{
    if (!_noticeArray) {
        _noticeArray = [[NSMutableArray alloc] init];
    }
    return _noticeArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"公告";
    self.view.backgroundColor = kColorWhite();
    [self initTableview];
    [self loadPublicNoticeData];
}

-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = false;
    }
    self.tableview.backgroundColor = UIColorHex(0xf5f5f5);
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerNib:[UINib nibWithNibName:@"PublicNoticeMineCell" bundle:nil] forCellReuseIdentifier:@"PublicNoticeMineCell"];
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).mas_equalTo(self.navH );
        make.bottom.equalTo(weakSelf.view).mas_equalTo(-self.BotH);
        make.left.right.equalTo(weakSelf.view);
    }];
 
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  
    return 85;

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.noticeArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NRWCarHomeNoticeModel *notieModel = self.noticeArray[indexPath.row];
    PublicNoticeMineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PublicNoticeMineCell"];
    cell.notieModel = notieModel;
    
    return cell;
}

-(void)loadPublicNoticeData{
    
    NSDictionary *params = @{@"marketId":[NRWCarUser sharedUser].marketId,
                             @"noticeState":@(1)
                             };
    [YQNetworking postWithUrl:[NSString stringWithFormat:@"%@%@",Api,publicNoticeApi] refreshRequest:YES cache:NO params:params progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        NSArray *noticeArr = response[@"result"];
        [self.noticeArray removeAllObjects];
        for (NSDictionary *dic in noticeArr) {
            NSDictionary *noticeDic =  dic[@"notice"];
            NRWCarHomeNoticeModel *notieModel = [[NRWCarHomeNoticeModel alloc] init];
            [notieModel setValuesForKeysWithDictionary:noticeDic];
            [self.noticeArray addObject:notieModel];
        }
        if (self.noticeArray.count == 0) {
            [self.tableview addDefaultViewWithText:@"暂无数据" defaultImageType:DefaultImageTypeNoData];
        }else{
            [self.tableview removeDefaultView];
        }
        
        [self.tableview reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
        
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];
    }];
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NRWCarHomeNoticeModel *notieModel = self.noticeArray[indexPath.row];

    NRWCarPublicNoticeController *detailVC = [[NRWCarPublicNoticeController alloc] init];
    detailVC.notieModel = notieModel;
    [self.navigationController pushViewController:detailVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
